package pl.agh.mikro.hovercraftcontroller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ControlActivity extends Activity {
	
	private BluetoothSocket btSocket = null;
	private OutputStream outStream = null;
	Button buttonStopsEverything,buttonMoveBack, buttonTurnRight,
		buttonTurnLeft, buttonPumpsIt, buttonMoveForward, buttonStopMoving;
	private ButtonClicked clicked;
    
	private static final UUID MY_UUID = UUID.randomUUID();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_control);
		buttonStopsEverything = (Button) findViewById(R.id.stopsEverything);
		buttonMoveBack = (Button) findViewById(R.id.moveBack);
		buttonTurnRight = (Button) findViewById(R.id.turnRight);
		buttonTurnLeft = (Button) findViewById(R.id.turnLeft);
		buttonPumpsIt = (Button) findViewById(R.id.pumpsIt);
		buttonMoveForward = (Button) findViewById(R.id.moveForward);
		buttonStopMoving = (Button) findViewById(R.id.stopMoving);
		clicked = new ButtonClicked();
		MyApp appState = ((MyApp)getApplicationContext());
		System.out.println("ControlActivity "+appState.getBluetoothDevice());
		try {
			btSocket = (appState.getBluetoothDevice()).createRfcommSocketToServiceRecord(MY_UUID);
			btSocket.connect();
			System.out.println("Log controlActivity connection socket: ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outStream = btSocket.getOutputStream();
			outStream.write(25); //wywalić :)

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		buttonStopsEverything.setOnClickListener(clicked);
		buttonMoveBack.setOnClickListener(clicked);
		buttonTurnRight.setOnClickListener(clicked);
		buttonTurnLeft.setOnClickListener(clicked);
		buttonPumpsIt.setOnClickListener(clicked);
		buttonMoveForward.setOnClickListener(clicked);
		buttonStopMoving.setOnClickListener(clicked);
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.control, menu);
		return true;
	}

	class ButtonClicked implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
